﻿//======================================================================
//
//        Copyright (C) 2015   
//        All rights reserved
//
//        filename : PutInBadCard
//        description : 制卡坏卡入库

//        created by XXX at  20150821
//
//======================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CardLogistics.Web.Components;
using System.Data;
using System.Text;

namespace CardLogistics.Web.MainPage.PutInCard.PutInBadCard
{
    public partial class AddEdit : BasePage
    {
        private static readonly CardLogistics.DAL.PutInCard.PutInBadCardData DAL =new DAL.PutInCard.PutInBadCardData ();
        private static readonly CardLogistics.DAL.PutInCard.PutInBadCard_DetailData DDAL = new DAL.PutInCard.PutInBadCard_DetailData ();
        private static readonly CardLogistics.DAL.CardMaking.CardMaking_CardOutStrgeDetailData OutDAL = new CardLogistics.DAL.CardMaking.CardMaking_CardOutStrgeDetailData();
        string badCardId = "";//坏卡入库Id
        public string TitleName
        {
            get;
            set;
        }
        #region 初始化
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                TitleName = GetStatusInfo(-1, "制卡坏卡入库");
                InitControl();
            }
        }
        #endregion

        #region 编辑时绑定值
        /// <summary>
        /// 编辑时绑定值
        /// </summary>
        public void InitControl()
        {
            if (IsEdit)
            {
                string _key = Request.QueryString["id"];
                if (!string.IsNullOrEmpty(_key))
                {
                    CardLogistics.Model.PutInCard.PutInBadCard pData = DAL.GetModel(_key);
                    SetWebControls(this.Page, pData);
                    hf_OutStorageId.Value = pData.OUTSTORAGEID; //白卡出库外键ID
                    DataTable dt = DDAL.GetInfoByBadCardId(pData.ID);
                    string detName = "";
                    string detId = "";
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        foreach (DataRow dr in dt.Rows)
                        {
                            detName += dr["ActualNumber"].ToString() + "[" + dr["StartCardNo"].ToString() + "]" + "[" + dr["EndCardNo"].ToString() + "];";
                            detId += dr["ActualNumber"].ToString() + "," + dr["StartCardNo"].ToString() + "," + dr["EndCardNo"].ToString() + ";";
                        }
                        hf_detailId.Value = detId.TrimEnd(';');
                    }
                }
            }
            else
            {
                BADCARDNO.Value = GetBillNumber("制卡坏卡入库");
            }

        }
        #endregion

        #region 保存事件
        /// <summary>
        /// 保存按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Add_Click(object sender, EventArgs e)
        {
            try
            {
                string result = EncapsulationModel();
                if (result.Split(',')[0] == "ok")
                {
                    badCardId = result.Split(',')[1]; //获取制卡坏卡Id
                    InsertLog("坏卡入库单号信息,坏卡入库单号:" + BADCARDNO.Value);
                    ReturnListInfo();
                }
                else if (result.Split(',')[1] == "-1")
                { 
                    ShowMessageBox("坏卡入库单号已存在，不允许重复！");
                    return;
                }
                else if (result.Split(',')[1] == "-2")
                {
                    ShowMessageBox("该卡片不存在，请重新输入！");
                    return;
                }
                else if (result.Split(',')[1] == "-3")
                {
                    ShowMessageBox("卡片总数量不能大于白卡出库实际数量！");
                    return;
                }
                else
                {
                    ShowMessageBox("操作失败请重新操作！" + result.Split(',')[1]);
                }
            }
            catch (Exception ex)
            {
                ShowMessageBox("系统异常:" + ex.ToString());
            }
        }
        #endregion

        #region 制卡坏卡入库信息
        /// <summary>
        /// 制卡坏卡入库信息
        /// </summary>
        /// <returns></returns>
        private string EncapsulationModel()
        {
            int startCardNo = Convert.ToInt32(hf_StartCardNo.Value); //开始卡号
            int endCardNo = Convert.ToInt32(hf_EndCardNo.Value); //结束卡号
            int cardNum = Convert.ToInt32(hf_CardNum.Value);//卡片总数量
            int cardOutNum = 0; //白卡出库实际数量

            string result = string.Empty;
            CardLogistics.Model.PutInCard.PutInBadCard model = new CardLogistics.Model.PutInCard.PutInBadCard();
            model = GetWebControls<CardLogistics.Model.PutInCard.PutInBadCard>(this.Page);
            model.LASTUPDATEUSER = LoggedUser.ID;
            model.LASTUPDATEDATE = DateTime.Now;
            model.ADDTIME = DateTime.Now;
            model.OUTSTORAGEID = hf_OutStorageId.Value; //白卡出库外键ID

            string FileValue = hf_detailId.Value; //制卡坏卡明细字段

            //通过白卡出库单号查询实际总数量
            DataTable dd = OutDAL.GetInfoByCheckId(hf_OutStorageId.Value);
            if (dd.Rows.Count > 0)
            {
                cardOutNum = Convert.ToInt32(dd.Compute("sum(ACTUALNUMBER)", ""));//白卡出库实际数量
            }
         
           StringBuilder strSql = new StringBuilder();
           strSql.Append(" select * from storage_detailed_card S, putinbadcard_detail ");
           strSql.Append("  where " + startCardNo + ">=S.STARTNO and " + endCardNo + "<=S.ENDNO");
      
            DataTable dt = CardLogistics.DAL.DbHelperSQL.GetQueryResult(""+strSql.ToString()+"");
            if (dt.Rows.Count==0)
            {
                result = "err,-2"; //该卡片不存在
            }
            else if (cardNum>cardOutNum)
            {
                result = "err,-3"; //卡片总数量不能大于白卡出库实际数量
            }
            else
            {
                if (IsEdit)
                {
                    model.ID = Request.QueryString["id"];
                    result = DAL.Update(model,FileValue);
                }
                else
                {
                    ChgBillNumber("制卡坏卡入库");
                    result = DAL.Add(model,FileValue);
                }
            }
           
            return result;
        }
        #endregion
    }
}